package com.mybatis_puls.demo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mybatis_puls.demo.mapper.UserMapper;
import com.mybatis_puls.demo.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

@SpringBootTest
public class WarpperUsertest {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void test1(){
        //查询nome不为空的参数 邮箱不为空  年龄大与2
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("name")
                .isNotNull("email")
                .ge("age",12); //ge  大于等于12
        List<User> users = userMapper.selectList(wrapper);
        users.forEach(System.out::println);
    }

    //查询一个数据
    @Test
    public void test2(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name","张三");  //eq 查询
        userMapper.selectOne(wrapper).toString();
    }

  //区间查询
    @Test
    public void test3(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between("age",20,30);   //between 之间   age:变量  20到30之间
        Integer count = userMapper.selectCount(wrapper);//查询数量
        System.out.println(count);
    }

    //包含查询
    @Test
    public void test4(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
           wrapper
                   .notLike("name","张")  //不包含查询
                   .likeRight("email","5"); //右开头包含
        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        maps.forEach(System.out::println);
    }

     //模糊查询
    @Test
    public void test5(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.inSql("id","select id from user where id<12");  //添加 sql语句查询
        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        maps.forEach(System.out::println);
    }

    //排序查询
     @Test
     public void test6(){
         QueryWrapper<User> wrapper = new QueryWrapper<>();
           wrapper.orderByDesc("id");  //降序
         List<User> users = userMapper.selectList(wrapper);
         users.forEach(System.out::println);
     }

}
